<?php
$MenuList = Yii::app()->params['menu'];
?>

<div class="main-content">
    <div class="section__content section__content--p20">
        <div class="container-fluid">
            <div class="row m-b-30">
                <div class="col-lg-12">
                    <div class="overview-wrap">
                        <h2 class="title-1"><?php echo $id == 0 ? "添加管理组" : "编辑管理组"; ?></h2>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-header">
                            <strong><?php echo $id == 0 ? "添加管理组" : "编辑管理组"; ?></strong>
                        </div>
                        <div class="card-body">
                            <form class="form-horizontal" role="form" name="form" id="form" method="post"
                                  data-bv-feedbackicons-valid="glyphicon glyphicon-ok"
                                  data-bv-feedbackicons-invalid="glyphicon glyphicon-remove"
                                  data-bv-feedbackicons-validating="glyphicon glyphicon-refresh"
                                  action="index.php?r=admin/role/edit" enctype="multipart/form-data">
                                <input type="hidden" name="id" value="<?php echo $id; ?>"/>
                                <input type="hidden" name="YII_CSRF_TOKEN" value="<?php echo Yii::app()->request->csrfToken; ?>"/>

                                <div class="form-group">
                                    <label class=" form-control-label">组名</label>
                                    <input type="text" name="rolename" value="<?php echo empty($role) ? "" : $role['rolename']; ?>"
                                           class="form-control">
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">全选</label>
                                    <input type="checkbox" name="permission_ckall" onclick="triggerCKAll()"/>
                                </div>

                                <?php
                                foreach ($MenuList as $m) {
                                    ?>
                                    <div class="form-group">
                                        <?php
                                        if (empty($permissions)) {
                                            ?>
                                            <label class="col-sm-2 control-label"><input type="checkbox" class="permission"
                                                                                         name="permission[]"
                                                                                         value="<?php echo $m['url']; ?>"/><?php echo $m['name']; ?>
                                            </label>
                                            <?php
                                        } else {
                                            $cked = in_array($m['url'], $permissions) ? "checked='checked'" : "";
                                            ?>
                                            <label class="col-sm-2 control-label"">
                                            <input <?php echo $cked; ?> class="permission"
                                                                        name="permission[]"
                                                                        type="checkbox"
                                                                        value="<?php echo $m['url']; ?>"/><?php echo $m['name']; ?>

                                            </label>
                                            <?php
                                        }
                                        if (sizeof($m['child']) > 0) {
                                        ?>
                                        <div class="col-sm-10">
                                            <?php
                                            foreach ($m['child'] as $c) {
                                                ?>
                                                <p>
                                                <?php

                                                if (empty($permissions)) {
                                                    ?>
                                                    <label class="control-label">
                                                        <input type="checkbox" class="permission" name="permission[]"
                                                               value="<?php echo $c['url']; ?>"/>&nbsp;&nbsp;<strong><?php echo $c['name']; ?></strong>
                                                    </label>
                                                    <?php
                                                } else {
                                                    $cked = in_array($c['url'], $permissions) ? "checked='checked'" : "";
                                                    ?>
                                                    <label class="control-label">
                                                        <input <?php echo $cked; ?> class="permission" name="permission[]"
                                                                                    type="checkbox"
                                                                                    value="<?php echo $c['url']; ?>"/>&nbsp;&nbsp;<strong><?php echo $c['name']; ?></strong>

                                                    </label>
                                                    <?php
                                                }
                                                if (sizeof($c['actions']) > 0) {
                                                    ?>
                                                    <p>
                                                        <?php
                                                        foreach ($c['actions'] as $a) {
                                                            if (empty($permissions)) {
                                                                ?>
                                                                <label class="control-label">
                                                                    <input type="checkbox" class="permission" name="permission[]"
                                                                           value="<?php echo $a['url']; ?>"/>&nbsp;&nbsp;<?php echo $a['name']; ?>
                                                                </label>
                                                                <?php
                                                            } else {
                                                                $cked = in_array($a['url'], $permissions) ? "checked='checked'" : "";
                                                                ?>
                                                                <label class="control-label">
                                                                    <input <?php echo $cked; ?> type="checkbox" class="permission"
                                                                                                name="permission[]"
                                                                                                value="<?php echo $a['url']; ?>"/>&nbsp;&nbsp;<?php echo $a['name']; ?>
                                                                </label>
                                                                <?php
                                                            }
                                                        }
                                                        ?>
                                                    </p>
                                                    <?php

                                                }

                                                ?>
                                                </p>
                                                <?php

                                            }
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    <?php
                                }
                                ?>

                                <div class="form-group">
                                    <button  type="submit" name="submit" id="submit" class="btn btn-primary btn-lg btn-block">保存</button>
                                </div>


                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        initPage();
    });
    function initPage() {
        $('#form').bootstrapValidator();
        $('#form').ajaxForm({
            dataType: 'json',
            beforeSubmit: function (formData, jqForm, options) {
                return $('#form').data('bootstrapValidator').isValid();
            },
            success: function (res) {
                if (message(res)) {
                    $("input[name=submit]").removeProp("disabled");
                }
            }
        });
    }
    function triggerCKAll() {
        var cked = $("input[name=permission_ckall]").prop("checked");
        if (cked == false) {
            $(".permission").removeProp("checked");
        } else {
            $(".permission").prop("checked", true);
        }
    }
</script>